package com.papercut.gui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.papercut.R;
import com.papercut.purchase.PurchaseActivity;
import com.papercut.purchase.PurchaseItem;
import com.papercut.service.PaperCutDatabase;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Notifications extends Activity {
	
	String transid;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LinearLayout notifications_view = new LinearLayout(this);
        notifications_view.setOrientation(LinearLayout.VERTICAL);
        notifications_view.setBackgroundResource(R.drawable.amazon_background_swipecheck);
		
		ScrollView notifications_scroll = new ScrollView(this);
		ViewGroup.LayoutParams scroll_layout = new ViewGroup.LayoutParams(480,680);
	
		
		LinearLayout scroll_linear = new LinearLayout(this);
		scroll_linear.setOrientation(LinearLayout.VERTICAL);
		
		List<String> transactionID = new ArrayList<String>();
		List<String> merchantName = new ArrayList<String>();
		List<Integer> invoiceDate = new ArrayList<Integer>();
		List<Integer> purchaseDate = new ArrayList<Integer>();
		List<Integer> hasPurchased = new ArrayList<Integer>();
		List<Float> totalCost = new ArrayList<Float>();
		
		PaperCutDatabase helper = new PaperCutDatabase(this);
		
		helper.getHistory(transactionID, merchantName, invoiceDate, purchaseDate, hasPurchased, totalCost);
		
		
		
		TextView current_notifications = new TextView(this);		
		LinearLayout.LayoutParams current_notifications_layout = new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		current_notifications_layout.setMargins(15, 20, 15, 0);
		current_notifications.setText("Open Purchases");
		current_notifications.setTextSize(20);
		current_notifications.setTypeface(Typeface.DEFAULT_BOLD);
		current_notifications.setTextColor(Color.rgb(231, 122, 0));
		current_notifications.setGravity(Gravity.LEFT);
		scroll_linear.addView(current_notifications, current_notifications_layout);
		


		Iterator itertransid = transactionID.iterator();
		Iterator itermerchant = merchantName.iterator();
		Iterator iterdate = invoiceDate.iterator();
		Iterator iterpurchase = purchaseDate.iterator();
		Iterator iterhaspurchase = hasPurchased.iterator();
		Iterator itercost = totalCost.iterator();
		
		
		
		

		while (itertransid.hasNext()) {
			transid = (String) itertransid.next();
			String merchant = (String) itermerchant.next();
			int date = (Integer) iterdate.next();
			int pdate = (Integer) iterpurchase.next();
			int hp = (Integer) iterhaspurchase.next();
			float cost = (Float) itercost.next();
			if (hp == 0) {
				TextView text = new TextView(this);
				text.setBackgroundColor(Color.rgb(240, 249, 255));
				LinearLayout.LayoutParams text_layout = new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				text_layout.setMargins(15, 0, 15, 0);
				text.setText(Html.fromHtml("<font color=#001870>" + "Merchant: " + "</font> <font color=#E77A00>" + merchant + "</font>" + "<br />" +
						"<font color=#001870>" + "Purchase Date: " + "</font> <font color=#E77A00>" + pdate + "</font>" + "<br />" +
						"<font color=#001870>" + "Cost: " + "</font> <font color=#E77A00>" + cost + "</font>"));
				text.setTextSize(16);
				text.setGravity(Gravity.LEFT);
				text.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent check = new Intent(Notifications.this, PurchaseActivity.class);
						check.putExtra("TRANS", transid);
						startActivity(check);
					}
					
				});
				scroll_linear.addView(text, text_layout);
				
				ImageView line = new ImageView(this);
				LinearLayout.LayoutParams line_layout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				line_layout.setMargins(15, 0, 15, 0);
				line.setImageResource(R.drawable.in_between_line);
				scroll_linear.addView(line, line_layout);	



			}
		}
		
		TextView past_notifications = new TextView(this);
		LinearLayout.LayoutParams past_notifications_layout = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		past_notifications_layout.setMargins(15, 20, 15, 0);
		past_notifications.setText("Past Purchases");
		past_notifications.setTextSize(20);
		past_notifications.setTypeface(Typeface.DEFAULT_BOLD);
		past_notifications.setTextColor(Color.rgb(231, 122, 0));
		past_notifications.setGravity(Gravity.LEFT);
		scroll_linear.addView(past_notifications, past_notifications_layout);
		
		 itertransid = transactionID.iterator();
		 itermerchant = merchantName.iterator();
		 iterdate = invoiceDate.iterator();
		 iterpurchase = purchaseDate.iterator();
		 iterhaspurchase = hasPurchased.iterator();
		 itercost = totalCost.iterator();
		
		
		
		

		while (itertransid.hasNext()) {
			transid = (String) itertransid.next();
			String merchant = (String) itermerchant.next();
			int date = (Integer) iterdate.next();
			int pdate = (Integer) iterpurchase.next();
			int hp = (Integer) iterhaspurchase.next();
			float cost = (Float) itercost.next();
			if (hp == 1) {
				TextView text = new TextView(this);
				text.setBackgroundColor(Color.rgb(240, 249, 255));
				LinearLayout.LayoutParams text_layout = new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				text_layout.setMargins(15, 0, 15, 0);
				text.setText(Html.fromHtml("<font color=#001870>" + "Merchant: " + "</font> <font color=#E77A00>" + merchant + "</font>" + "<br />" +
						"<font color=#001870>" + "Purchase Date: " + "</font> <font color=#E77A00>" + pdate + "</font>" + "<br />" +
						"<font color=#001870>" + "Cost: " + "</font> <font color=#E77A00>" + cost + "</font>"));
				text.setTextSize(16);
				text.setGravity(Gravity.LEFT);

				scroll_linear.addView(text, text_layout);
				
				ImageView line = new ImageView(this);
				LinearLayout.LayoutParams line_layout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				line_layout.setMargins(15, 0, 15, 0);
				line.setImageResource(R.drawable.in_between_line);
				scroll_linear.addView(line, line_layout);	



			}
		}
	
		
		
		
		notifications_scroll.addView(scroll_linear);
		
		notifications_view.addView(notifications_scroll, scroll_layout);
		
		ImageView dashboard_button = new ImageView(this);
		LinearLayout.LayoutParams dashboard_button_layout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		dashboard_button_layout.setMargins(104, 25, 104, 0);
		dashboard_button.setImageResource(R.drawable.dash_board_button);
		dashboard_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
			
		});
		notifications_view.addView(dashboard_button, dashboard_button_layout);	
		
		setContentView(notifications_view);

    }

}
